/*
 * @Description: In User Settings Edit
 * @Author: your name
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-04-05 21:34:29
 */
import { toRaw } from "vue";
import { getAddressList, getUser } from "@/api";
//引入local
import { getStore, setStore } from "@/utils/commonApi";

const actions = {
  // 记录当前经度纬度
  recordAddressFn({ latitude, longitude }) {
    this.latitude = latitude;
    this.longitude = longitude;
  },
  //保存geohash
  saveGeohashFn(geohash) {
    this.geohash = geohash;
  },
  // 记录用户信息
  recordUserinfoFn(info) {
    this.userInfo = info;
    this.login = true;
    setStore("user_id", info.user_id);
  },
  //获取用户信息存入vuex
  getUserinfo(info) {
    if (this.userInfo && this.userInfo.username !== info.username) {
      return;
    }
    if (!this.login) {
      return;
    }
    if (!info.message) {
      this.userInfo = {
        ...info,
      };
    } else {
      this.userInfo = null;
    }
  },
  recordShopDetailFn(detail) {
    this.shopDetail = detail;
  },
  // 加入购物车
  addCartFn({
    shopid,
    category_id,
    item_id,
    food_id,
    name,
    price,
    specs,
    packing_fee,
    sku_id,
    stock,
  }) {
    let cart = this.cartList;
    let shop = (cart[shopid] = cart[shopid] || {});
    let category = (shop[category_id] = shop[category_id] || {});
    let item = (category[item_id] = category[item_id] || {});
    if (item[food_id]) {
      item[food_id]["num"]++;
    } else {
      item[food_id] = {
        num: 1,
        id: food_id,
        name: name,
        price: price,
        specs: specs,
        packing_fee: packing_fee,
        sku_id: sku_id,
        stock: stock,
      };
    }
    this.cartList = {
      ...cart,
    };
    //存入localStorage
    setStore("buyCart", this.cartList);
  },
  // 移出购物车
  reduceCartFn({ shopid, category_id, item_id, food_id, name, price, specs }) {
    let cart = this.cartList;
    let shop = cart[shopid] || {};
    let category = shop[category_id] || {};
    let item = category[item_id] || {};
    if (item && item[food_id]) {
      if (item[food_id]["num"] > 0) {
        item[food_id]["num"]--;
        this.cartList = {
          ...cart,
        };
        //存入localStorage
        setStore("buyCart", this.cartList);
      } else {
        //商品数量为0，则清空当前商品的信息
        item[food_id] = null;
      }
    }
  },
  //网页初始化时从本地缓存获取购物车数据
  initBuyCartFn() {
    let initCart = getStore("buyCart");
    if (initCart) {
      this.cartList = JSON.parse(initCart);
    }
  },
  //清空当前商品的购物车信息
  clearCartFn(shopid) {
    this.cartList[shopid] = null;
    this.cartList = {
      ...this.cartList,
    };
    setStore("buyCart", this.cartList);
  },
  //保存商铺id
  saveShopIdFn(shopid) {
    this.shopid = shopid;
  },
  //选择的地址
  chooseAddressFn({ address, index }) {
    this.choosedAddress = address;
    this.addressIndex = index;
  },
  //保存下单需要验证的返回值
  needValidationFn(needValidation) {
    this.needValidation = needValidation;
  },
  //保存下单后购物id 和 sig
  saveCartIdSigFn({ cart_id, sig }) {
    this.cart_id = cart_id;
    this.sig = sig;
  },
  //保存下单参数，用户验证页面调用
  saveOrderParamFn(orderParam) {
    this.orderParam = orderParam;
  },
  //修改下单参数
  changeOrderParamFn(newParam) {
    this.orderParam = Object.assign({}, this.orderParam, newParam);
  },
  orderSuccessFn(order) {
    this.cartPrice = null;
    this.orderMessage = order;
  },
  // 	//是否开发票
  // [CONFIRM_INVOICE]( invoice) {
  // 	this.invoice = invoice;
  // },
  //选择搜索的地址
  chooseSearchAddressFn(place) {
    this.searchAddress = place;
  },
  //记录订单页面用户选择的备注, 传递给订单确认页面
  confirmRemarkFn({ remarkText, inputText }) {
    this.remarkText = remarkText;
    this.inputText = inputText;
  },
  //确认订单页添加新的的地址
  confirmAddressFn(newAddress) {
    this.newAddress.push(newAddress);
  },
  //进入订单详情页前保存该订单信息
  saveOrderFn(orderDetail) {
    this.orderDetail = orderDetail;
  },
  //退出登录
  outLoginFn(state) {
    this.userInfo = {};
    this.login = false;
  },
  //保存图片
  saveAvanderFn(imgPath) {
    this.imgPath = imgPath;
  },
  //修改用户名
  retsetNameFn(username) {
    this.userInfo = Object.assign({}, this.userInfo, { username });
  },
  //删除地址列表
  saveAddressFn(newAdress) {
    this.removeAddress = newAdress;
  },
  //添加地址name
  saveAddDetailFn(addAddress) {
    this.addAddress = addAddress;
  },
  //增加地址
  addAddressFn(obj) {
    this.removeAddress = [obj, ...this.removeAddress];
  },
  async getUserInfoFn() {
    if (this.userInfo) return;
    let res = await getUser();
    this.getUserinfo(res);
  },
  async toSaveAddressFn() {
    if (this.removeAddress.length > 0) return;
    let addres = await getAddressList(this.userInfo.user_id);
    this.saveAddressFn(addres);
  },
};
export default actions;
